package online.exam.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import online.exam.common.TeacherStateEnum;
import online.exam.common.YesOrNo;
import online.exam.mapper.TeacherMapper;
import online.exam.pojo.Teacher;
import online.exam.pojo.bo.TeacherBO;
import online.exam.pojo.vo.TeacherVO;
import online.exam.service.TeacherService;
import online.exam.utils.MD5Tools;
import online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 王景煜
 */

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Teacher queryByNoAndPwd(TeacherBO teacherBO) {
        Example teaExample = new Example(Teacher.class);
        Example.Criteria teaCriteria = teaExample.createCriteria();
        teaCriteria.andEqualTo("teacherNo", teacherBO.getTeaNo());
        teaCriteria.andEqualTo("password", MD5Tools.md5EncodingUtf8(teacherBO.getTeaPwd()));
        teaCriteria.andEqualTo("state", TeacherStateEnum.ONLINE.type);
        Teacher queryTeacher = teacherMapper.selectOneByExample(teaExample);
        if (queryTeacher != null) {
            queryTeacher.setPassword(null);
            return queryTeacher;
        }
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageUtils<TeacherVO> queryAll(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<TeacherVO> teacherVOList = teacherMapper.queryAllTeacher();
        PageInfo<?> info = new PageInfo<>(teacherVOList);
        PageUtils<TeacherVO> teacherPageUtils = new PageUtils<>();
        teacherPageUtils.setPage(page);
        teacherPageUtils.setLimit(limit);
        teacherPageUtils.setCount(info.getTotal());
        teacherPageUtils.setData(teacherVOList);
        return teacherPageUtils;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Teacher queryById(String id) {
        return teacherMapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Teacher queryByNo(String teacherNo) {
        Example ste = new Example(Teacher.class);
        Example.Criteria stc = ste.createCriteria();
        stc.andEqualTo("teacherNo", teacherNo);
        return teacherMapper.selectOneByExample(ste);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Teacher> queryByCondition(Teacher teacher) {
        Example teaExample = new Example(Teacher.class);
        Example.Criteria teaCriteria = teaExample.createCriteria();
        if (!StringUtils.isEmpty(teacher.getId()))
            teaCriteria.andEqualTo("id", teacher.getId());
        if (!StringUtils.isEmpty(teacher.getTeacherNo()))
            teaCriteria.andEqualTo("teacherNo", teacher.getTeacherNo());
        if (!StringUtils.isEmpty(teacher.getName()))
            teaCriteria.andEqualTo("name", teacher.getName());
        if (!StringUtils.isEmpty(teacher.getClazz()))
            teaCriteria.andEqualTo("clazz", teacher.getClazz());
        return teacherMapper.selectByExample(teaExample);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addTeacher(Teacher teacher) {
        teacher.setId(IdUtil.simpleUUID());
        teacher.setPassword(MD5Tools.md5EncodingUtf8("123456"));
        teacher.setIsAdmin(YesOrNo.NO.type);
        teacher.setLogo("http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png");
        teacher.setState(TeacherStateEnum.ONLINE.type);
        teacher.setCreatedTime(new Date());
        int result = teacherMapper.insert(teacher);
        return result > 0 ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateTeacher(Teacher teacher) {
        if (!StringUtils.isEmpty(teacher.getPassword()))
            teacher.setPassword(MD5Tools.md5EncodingUtf8(teacher.getPassword()));
        int result = teacherMapper.updateByPrimaryKeySelective(teacher);
        return result > 0 ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int deleteTeacher(String id) {
        if (id.contains(",")) {
            String[] ids = id.split("\",\"");
            List<String> idList = new ArrayList<>(Arrays.asList(ids));
            int result = teacherMapper.deleteMorePojo(idList);
            return ids.length == result ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
        }
        int result = teacherMapper.deletePojo(id);
        return result > 0 ? HttpStatus.HTTP_OK : HttpStatus.HTTP_INTERNAL_ERROR;
    }

}
